/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package domain.taskManagement.controllers;

import domain.taskManagement.exceptions.DuplicateTaskException;
import domain.taskManagement.exceptions.TaskNotFoundException;
import domain.taskManagement.models.Task;
import domain.userManagement.models.User;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author peterfrem
 */
public class TaskManager {
    
    public static void reassignTask(Task task, User currentOwner, User newOwner) 
	    throws TaskNotFoundException, DuplicateTaskException{
	currentOwner.getTasks().removeTask(task);
		
	newOwner.getTasks().addTask(task);
    }
    
    public static void createTask(User currentUser, String title, String description, Date dueDate, int priority){
	//TODO: generate unique ID
	int id = 0;
	Date today = new Date();
	
	Task task = new Task(id, title, description, today, dueDate, priority);
	
	try {
	    currentUser.getTasks().addTask(task);
	} catch (DuplicateTaskException ex) {
	    //if this happens, the ID generate was not unique
	    //need to generate a new one
	    task.setId(id);
	    
	    //try to add task to user with new id
	    try {
		currentUser.getTasks().addTask(task);
	    } catch (DuplicateTaskException ex1) {
		Logger.getLogger(TaskManager.class.getName()).log(Level.SEVERE, null, ex1);
	    }

	}
    }
    
}
